<?php


namespace app\back\model;

use think\facade\Db;
use think\response\Json;
use \Exception;

class GoodsReplyModel
{
    private static $_table = 'goods_reply'; // 订单评价表

    /**获取订单评价列表
     * @param $input
     * @param $info
     * @return Json
     */
    public static function list($input,$info)
    {
        try {
            $map = [
                ['admin_id', '=', $info['id']]
            ];
            if ($input['status'] !== '') {
                $map[] = ['status', '=', $input['status']];
            }
            if ($input['is_reply'] !== '') {
                $map[] = ['is_reply', '=', $input['is_reply']];
            }
            $field = 'id,uid,oid,goods_id,goods_score,service_score,comment,pics,add_time,merchant_reply_content,merchant_reply_time,status,is_reply';
            $table = Db::table(self::$_table)
                ->where($map)->field($field);
            $count = $table->count();
            if ($count == 0) return jsonSuccess('暂无数据', ['count' => 0,'list' => []]);
            $list = $table->page($input['page'], $input['limit'])->order(['is_reply' => 'asc','add_time' => 'desc'])->select()->toArray();
            return jsonSuccess('获取成功', ['count' => $count, 'list' => $list]);
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**修改显示状态
     * @param $input
     * @param $info
     * @return Json
     */
    public static function changeStatus($input,$info)
    {
        try {
            $map = [
                ['id', '=', $input['id']],
                ['admin_id', '=', $info['id']],
                ['status','=', $input['status'] == 1? 0:1]
            ];
            $rtn = Db::table(self::$_table)->where($map)->update(['status' => $input['status']]);
            if ($rtn) return jsonSuccess('操作成功');
            return jsonFail('操作失败,刷新页面重试');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**回复评价
     * @param $input
     * @param $info
     * @return Json
     */
    public static function back($input,$info)
    {
        try {
            $map = [
                ['id', '=', $input['id']],
                ['admin_id', '=', $info['id']]
            ];
            $rtn = Db::table(self::$_table)->where($map)->update([
                'merchant_reply_content' => $input['merchant_reply_content'],
                'merchant_reply_time' => time(),
                'is_reply' => 1
            ]);
            if ($rtn) return jsonSuccess('回复成功');
            return jsonFail('回复失败,刷新页面重试');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}